package com.chcit.handmedicine.database.service;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.chcit.handmedicine.Constants;

public class BaseService extends SQLiteOpenHelper{

	protected SQLiteDatabase _db;
	
	private String _sqlCreateTable;
	private String _sqlDropTable;
	
	public BaseService(Context context) {
		super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
		_db = this.getWritableDatabase();
	}

	public BaseService(Context context, String sqlCreateTable, String sqlDropTable) {
		super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
		_sqlCreateTable = sqlCreateTable;
		_sqlDropTable = sqlDropTable;
		_db = this.getWritableDatabase();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		if (_sqlCreateTable != null)
			db.execSQL(_sqlCreateTable);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w("DBHelper", "Upgrading database from version " + oldVersion + " to " + newVersion);
		if (_sqlDropTable != null)
			db.execSQL(_sqlDropTable);
		onCreate(db);
	}

	public void close() {
		if (_db != null) {
			_db.close();
		}
	}
	
}
